package com.liuboya.service.weiboapi;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.liuboya.common.OkhttpUtils;
import com.liuboya.weibomodel.model.common.http.FromBodyEntry;
import com.liuboya.weibomodel.model.common.http.RequestHeader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：
 *
 * @author yeyuan
 * @date 2021/08/30 13:57
 **/
@Service
@Slf4j
public class WbApiServiceImpl implements WbApiService {
    @Override
    public JSONObject feed() {
        String url = "https://api.weibo.cn/2/guest/timeline?gsid=_2AkMWcUf4f8NhqwFRmPwWyWzraoV0wwvEieKgLbYjJRM3HRl-wT9kqnYotRV6PclLzz8RtN9jEJHkEZrkHEAYAcXRSTOF&uid=2004357968393&wm=3333_2001&launchid=10000365--x&b=0&from=10B8193010&c=iphone&networktype=wifi&skin=default&v_p=89&s=54fc8ab4&v_f=1&did=5f394bc74659183d947abc88f960e1d7&lang=zh_CN&sflag=1&ua=iPhone7,2__weibo__11.8.1__iphone__os12.5.4&ft=0";
        List<RequestHeader> headers = Lists.newArrayListWithExpectedSize(4);
        headers.add(new RequestHeader("content-type", "application/x-www-form-urlencoded; charset=utf-8"));
        headers.add(new RequestHeader("Host", "api.weibo.cn"));
        headers.add(new RequestHeader("user-agent", "Weibo/57986 (iPhone; iOS 12.5.4; Scale/2.00)"));
        headers.add(new RequestHeader("accept", "*/*"));
        List<FromBodyEntry> entries = Lists.newArrayListWithExpectedSize(6);
        entries.add(new FromBodyEntry("refresh", "pulldown"));
        entries.add(new FromBodyEntry("show_toplist", "0"));
        entries.add(new FromBodyEntry("image_type", "heif"));
        entries.add(new FromBodyEntry("moduleID", "feed"));
        entries.add(new FromBodyEntry("count", "25"));
        entries.add(new FromBodyEntry("trim_level", "1"));
        entries.add(new FromBodyEntry("trim_page_recom", "0"));
        entries.add(new FromBodyEntry("since_id", "0"));
        entries.add(new FromBodyEntry("isDailyFirstReload", "1"));
        String s = OkhttpUtils.commonPost(url, entries, headers, null);
        if (StringUtils.isBlank(s)) {
            log.error("微博API调用出错,feed流无数据");
        }
        return JSONObject.parseObject(s);
    }
}